var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
function query(){
	var htmlStr="<tr>  "
		+"  <th  width='90'  w_check='true' w_index='id' id='qx'></th>  "  				
		+"  <th  width='280' w_index='menuName'>菜单名称</th>  "
		+"	<th  width='240' w_index='sysName'>系统名称</th>  "
		+"	<th  width='150' w_index='sortNum'>排序号</th>  "
		+"	<th  width='200' w_render='operate' >操作</th>  "
		+"</tr>  ";
	$("#g_tabYeh").html(htmlStr);
	sdfs=[];
	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		/*
		 * dataType: 'json', ajaxType: 'post', url: '/user/getuserlist1',
		 */
		autoLoad : true,
		pageSizeSelect : false,
		stripeRows : true,
		pageSize : 999999999,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar : false
	});

	var userdatas = [];
	userdatas.success = true;
	userdatas.curPage = 1;
	var sysId=$("#sysId").val();
	var data_formate = '{"data":{"total":6,"curPage":1,"rows":';
	$.ajax({
		type : 'post',
		url : server_url+'/rest/menu/menuList.do?ran='+Math.random()+"&token="+token,
		data:{sysId:sysId},
		async:false,
		success : function(data) {
			if(data.retCode=="0000"||data.retCode=="0002"){
				var datastr = "";
				if(data.data!=null){
					datastr=data.data
				}
				var treejson = JSON.stringify(datastr);
				data_formate = data_formate+ treejson;
			}
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});
				
			} 
			
		}
	});
	data_formate = data_formate + '},"retCode":"0000","retMsg":"\u64cd\u4f5c\u6210\u529f"}';
	userdata = $.parseJSON(data_formate);
	getItemData(userdata.data.rows, 0, 0);
	userdatas.totalRows = sdfs.length;
	userdatas.data = sdfs;
	gridObj.loadGridData('json', userdatas);
	$('.parent0')
			.each(
					function() {
						var html_text = $(this).find("td").eq(1).html();
						html_text = '<div style="text-align:left;"><img src="images/g_remoJian.png" style="padding-left:8px;"/><span style="padding-left:10px;">'
								+ html_text + '</span></div>';
						$(this).find("td").eq(1).html(html_text);
						var id = $(this).attr('id');
						$("tr[parentId='" + id + "']")
								.each(
										function(i, v) {
											if ($(v).attr('node') == ''
													|| $(v).attr('node') == undefined) {
												var html_text = $(v).find("td")
														.eq(1).html();
												html_text = '<div style="text-align:left;"><span style="padding-left:40px;">'
														+ html_text
														+ '</span></div>';
												$(this).find("td").eq(1).html(
														html_text);
											}
											$(v).find("td").eq(1).css(
													'padding-left', '30px');

										});
					});
	$('.parent1')
			.each(
					function() {
						var html_text = $(this).find("td").eq(1).html();
						html_text = '<div style="text-align:left;"><img src="images/g_remoJian.png" style="padding-left:10px;"/><span style="padding-left:12px;">'
								+ html_text + '</span></div>';
						$(this).find("td").eq(1).html(html_text);
						var id = $(this).attr('id');
						$("tr[parentId='" + id + "']")
								.each(
										function(i, v) {
											if ($(v).attr('node') == ''
													|| $(v).attr('node') == undefined) {
												var html_text = $(v).find("td")
														.eq(1).html();
												html_text = '<div style="text-align:left;"><span style="padding-left:43px;">'
														+ html_text
														+ '</span></div>';
												$(this).find("td").eq(1).html(
														html_text);
											}
											$(v).find("td").eq(1).css(
													'padding-left', '60px');

										});
					});
	$('.parent2')
			.each(
					function() {
						var html_text = $(this).find("td").eq(1).html();
						html_text = '<div style="text-align:left;"><img src="images/g_remoJian.png" style="padding-left:12px;"/><span style="padding-left:14px;">'
								+ html_text + '</span></div>';
						$(this).find("td").eq(1).html(html_text);
						var id = $(this).attr('id');
						$("tr[parentId='" + id + "']")
								.each(
										function(i, v) {
											if ($(v).attr('node') == ''
													|| $(v).attr('node') == undefined) {
												var html_text = $(v).find("td")
														.eq(1).html();
												html_text = '<div style="text-align:left;"><span style="padding-left:46px;">'
														+ html_text
														+ '</span></div>';
												$(this).find("td").eq(1).html(
														html_text);
											}
											$(v).find("td").eq(1).css(
													'padding-left', '90px');

										});
					});
	$('.parent3')
			.each(
					function() {
						var html_text = $(this).find("td").eq(1).html();
						html_text = '<div style="text-align:left;"><img src="images/g_remoJian.png" style="padding-left:14px;"/><span style="padding-left:16px;">'
								+ html_text + '</span></div>';
						$(this).find("td").eq(1).html(html_text);
						var id = $(this).attr('id');
						$("tr[parentId='" + id + "']")
								.each(
										function(i, v) {
											if ($(v).attr('node') == ''
													|| $(v).attr('node') == undefined) {
												var html_text = $(v).find("td")
														.eq(1).html();
												html_text = '<div style="text-align:left;"><span style="padding-left:49px;">'
														+ html_text
														+ '</span></div>';
												$(this).find("td").eq(1).html(
														html_text);
											}
											$(v).find("td").eq(1).css(
													'padding-left', '120px');

										});
					});
	$('.parent4')
			.each(
					function() {
						var html_text = $(this).find("td").eq(1).html();
						html_text = '<div style="text-align:left;"><img src="images/g_remoJian.png" style="padding-left:16px;"/><span style="padding-left:18px;">'
								+ html_text + '</span></div>';
						$(this).find("td").eq(1).html(html_text);
						var id = $(this).attr('id');
						$("tr[parentId='" + id + "']")
								.each(
										function(i, v) {
											if ($(v).attr('node') == ''
													|| $(v).attr('node') == undefined) {
												var html_text = $(v).find("td")
														.eq(1).html();
												html_text = '<div style="text-align:left;"><span style="padding-left:52px;">'
														+ html_text
														+ '</span></div>';
												$(this).find("td").eq(1).html(
														html_text);
											}
											$(v).find("td").eq(1).css(
													'padding-left', '150px');

										});
					});

    for (var i = 0; i <5; i++) {
        $("tr[class='parent"+i+"']").each(function(p,k){
            $(k).find('td').eq(1).bind('click',function(){
                  var id=$(this).parent().attr('id');
                        if($(this).find('img').attr('src')=='images/g_remoJian.png'){
                           itemwind(id,'none');
                          $(this).find('img').attr('src','images/g_addJia.png');
                      }else{
                         $("tr[parentId='"+id+"']").each(function(i,v){
                            $(v).css('display','');
                          });
                        $(this).find('img').attr('src','images/g_remoJian.png');
                      }
            })

        });
      }
    $("tr[class='parent_left']").each(function(pp,kk){
        if($(kk).attr('parentId')==undefined || $(kk).attr('parentId')==''||$(kk).attr('parentId')=='0'){
            var html_text=$(kk).find("td").eq(1).html();
         html_text ='<div style="text-align:left;"><span style="padding-left:25px;width:25px;height:17px;"></span><!--<img src="/public/images/g_remoJian.png" style="padding-left:8px;"/>--><span style="padding-left:10px;">'+html_text+'</span></div>';
          $(kk).find("td").eq(1).html(html_text);
        }
    });
	$('#qx').append('<span style="padding-left:5px;">全选</span>');
	
	$('.l_pageUpDown').hide();
}

// 展示收缩
function itemwind(id, style) {
	$("tr[parentId='" + id + "']").each(
			function(i, v) {
				if (style == 'none') {
					if ($(v).attr('node') != ''
							&& $(v).attr('node') != undefined) {
						$(v).find("td").eq(1).find('img').attr('src',
								'images/g_addJia.png');
					}
				}
				$(v).css('display', style);
				var ids = $(this).attr('id');
				itemwind(ids, style);
			});
}
$.bsgrid.forcePushPropertyInObject(
		$.fn.bsgrid.defaults.extend.renderPerRowMethods,
		'extend_render_per_row', function(record, rowIndex, trObj, options) {
			if (record != undefined && record != '') {
				trObj.attr('id', record.id);
			}
			// trObj.find("td").first().css('text-align','left');
			if (record.node != undefined) {
				switch (record.node) {
				case 0:
					trObj.attr('class', 'parent0');
					trObj.attr('node', 0);
					break;
				case 1:
					trObj.attr('class', 'parent1');
					trObj.attr('node', 1);
					break;
				case 2:
					trObj.attr('class', 'parent2');
					trObj.attr('node', 2);
					break;
				case 3:
					trObj.attr('class', 'parent3');
					trObj.attr('node', 3);
					break;
				case 4:
					trObj.attr('class', 'parent4');
					trObj.attr('node', 4);
					break;
				}

			}
			else{
	               trObj.attr('class','parent_left');
	            }

			if (record != undefined && record != '' && record.parentId != ''
					&& record.parentId != undefined) {
				trObj.attr('parentId', record.parentId);
				// trObj.attr('class','parent_class');
			}
		});
var sdfs = [];
function getItemData(data, parentId, level) {
	if (typeof (data) == "object") {
		$.each(data, function(i, n) {
			if (parentId != 0) {
				n.parentId = parentId;
			}
			if (n.child != undefined && n.child != ""
					&& typeof (n.child) == 'object') {
				// console.log(n.child);
				n.node = level;
				sdfs.push(n);
				getItemData(n.child, n.id, level + 1);
			} else {
				sdfs.push(n);
			}
		});
	}
}

function operate(record, rowIndex, colIndex, options) {
	if(record.level=="1"||record.level=="2"){
		return '<a href="javascript:edit(\''
		+ gridObj.getRecordIndexValue(record, 'id')
		+ '\');">编辑</a>';
	}
	else if(record.level=="3"){
		return '<a href="javascript:edit(\''
		+ gridObj.getRecordIndexValue(record, 'id')
		+ '\');">编辑</a><a class="g_cianGya" href="javascript:oper(\''
		+ gridObj.getRecordIndexValue(record, 'id') + '\');">操作管理</a>';
	}
	
}
// 获取checkbox选中
function getCheckedIds() {
	// values are array
	return gridObj.getCheckedValues('id');
}
// 数据表格参数查询
function doSearch() {
	//console.log(searchParames);
}


//添加
function toAdd(){
	var url = "menuadd.do?ran="+Math.random()+"&token="+token;
	tl_config.loadToWorkArea(url);
}

//删除
function toDel(){
	var idArr=getCheckedIds();
	var menuId=idArr.toString();
	if(menuId!=null&&menuId!=""){
		easyDialog.open({
			  container : {
				  header : '您确定要删除吗？',
			    content : '您确定要删除吗？',
			    yesFn : function(){
			    	$.ajax({
						url:server_url+'/rest/menu/delMenu.do?ran='+Math.random()+"&token="+token,
						type:'get',
						data:{menuId:menuId},
						success:function(data){
							if(data.retCode=="0000"){
								query();
							}
							else{
								easyDialog.open({
									  container : {
										  header : '提示',
									    content : data.retMsg
									  },
									  autoClose : 2000
									});
								
							} 
							
						}
					})
			    },
			    noFn : true
			  }
			});
		
		
	}
	else{
		easyDialog.open({
			  container : {
				  header : '温馨提示',
			    content : '请选择要操作的记录！'
			  },
			  autoClose : 2000
			});
	}
	
}

//编辑
function edit(curid){
	var url ='menuedit.do?ran='+Math.random()+'&token='+token+'&curid='+curid;
	tl_config.loadToWorkArea(url);
}

//操作管理
function oper(curid){
	var url = 'operationList.do?menuId='+curid+'&token='+token+'&ran='+Math.random();
	tl_config.loadToWorkArea(url);
}

$(function() {
	//系统下拉框初始化
	$.ajax({
		url:server_url+'/rest/menu/getsysinfo.do?ran='+Math.random()+"&token="+token,
		type:'get',
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#sysId").append("<option value='"+data.data[i].id+"'>"+data.data[i].sysName+"</option>");
				}
			}
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});
				
			} 
			
		}
	});
	query();
	//查询事件
	$(".z_query").click(function(){
		query();
	});
	
});